package com.ants.auth.service;

import com.ants.auth.config.user.SecurityUser;
import com.ants.auth.config.user.UserDto;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 蚂蚁会花呗
 * @create 2025/5/29 13:42
 */
@Service
public class UserServiceImpl implements UserDetailsService {

    private List<UserDto> userList;

    @PostConstruct
    public void initData() {
        String password = new BCryptPasswordEncoder().encode("123456");
        userList = new ArrayList<>();
        userList.add(new UserDto(1,"macro", password,"18698589620", Arrays.asList("ROLE_ADMIN")));
        userList.add(new UserDto(2,"andy", password,"18698589620", Arrays.asList("ROLE_TEST","ROLE_USER")));
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<UserDto> findUserList = userList.stream().filter(item -> item.getUsername().equals(username)).collect(Collectors.toList());
        if (findUserList.isEmpty()) {
            throw new UsernameNotFoundException("用户名或密码错误！");
        }
        UserDto user = findUserList.get(0);
        List<GrantedAuthority> authorities = user.getRoles().stream()
                .map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        SecurityUser securityUser = getSecurityUser(user, authorities);
        return securityUser;
    }

    private static SecurityUser getSecurityUser(UserDto user, List<GrantedAuthority> authorities) {
        SecurityUser securityUser = new SecurityUser(user.getUsername(), user.getPassword(), authorities);
        if (!securityUser.isEnabled()) {
            throw new DisabledException("账户已禁用");
        } else if (!securityUser.isAccountNonLocked()) {
            throw new LockedException("账户已锁定");
        } else if (!securityUser.isAccountNonExpired()) {
            throw new AccountExpiredException("用户账户已经过期");
        } else if (!securityUser.isCredentialsNonExpired()) {
            throw new CredentialsExpiredException("用户凭证已过期");
        }
        securityUser.setUid(user.getUid());
        securityUser.setUsername(user.getUsername());
        return securityUser;
    }
}
